﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

namespace WebApp.Controls
{
    public partial class HotSaleForCategory : System.Web.UI.UserControl
    {
        ProductManager pm = new ProductManager();
        CategoryManager m_CategoryManager = new CategoryManager();
        private int showcount;

        public int ShowCount
        {
            get { return showcount; }
            set { showcount = value; }
        }

        private string categoryId;

        public string CategoryId
        {
            get { return categoryId; }
            set { categoryId = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void bind(bool IsSupCategory)
        {
            Category firstCategory = null;
            string firstCategoryId = categoryId;
            //所有子分类
            List<Category> subCategory = new List<Category>();
            while (true)
            {
                firstCategory = this.m_CategoryManager.GetCategoryById(new Guid(firstCategoryId));
                
                if (firstCategory.SupCategory.Name.Equals("Category_L"))
                {
                    
                    break;
                }
                else
                {
                    firstCategoryId = firstCategory.SupCategory.CategoryId.ToString();
                    
                }
            }
            subCategory = firstCategory.SubCategories.ToList();
           

           
            List<Cosmetic.DataEntity.Product> currentList = new List<Cosmetic.DataEntity.Product>(); //保存热销商品。
            List<ProductInfo> temListPro = new List<ProductInfo>();


            /////所有热销商品
            List<Cosmetic.DataEntity.RecommendProduct> recommendPro = pm.GetRecommendProducts();
            if (recommendPro.Count <= 0) return;

            foreach (Cosmetic.DataEntity.RecommendProduct item in recommendPro)
            {
                if (item.Product.ProductCategoryId.ToString() == firstCategoryId)
                {
                    currentList.Add(item.Product);
                }
                //从子类中得到热销商品
                foreach (Category subItem in subCategory)
                {
                    if (item.Product.ProductCategoryId.ToString() == subItem.CategoryId.ToString())
                    {
                        currentList.Add(item.Product);
                    }
                }
            }


            List<ProductInfo> theProductInfo = new List<ProductInfo>();
            try
            {
                foreach (Cosmetic.DataEntity.Product item in currentList)
                {
                    List<ProductInfo> temproinfor=pm.GetProductInfoByProductId(item.ProductId);
                    if (temproinfor.Count > 0)
                        theProductInfo.Add(temproinfor.First());
                }

            }
            catch
            { }



            if (theProductInfo.Count > 5)
            {
                this.lvProduct.DataSource = theProductInfo.Take(5);
                this.lvProduct.DataBind();
            }
            else
            {
                this.lvProduct.DataSource = theProductInfo;
                this.lvProduct.DataBind();
            }

           
            //}
        }
    }
}